<template>
  <!-- 主体区域 -->
  <section id="app">
    <TodoHeader @add="add"></TodoHeader>
    <TodoMain :list="list" @del="del"></TodoMain>
    <TodoFooter :list="list" @cal="list = []"></TodoFooter>
  </section>
</template>

<script>

import TodoHeader from './components/TodoHeader.vue';
import TodoMain from './components/TodoMain.vue';
import TodoFooter from './components/TodoFooter.vue';

export default {
  data () {
    return {
      list: JSON.parse(localStorage.getItem('list')) || [

        { id: 1, name: '168大长腿' },
        { id: 2, name: '真好看' },
        { id: 3, name: '蒸的C' }
      ]
    }
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter
  },
  methods: {
    add (obj) {
      if (obj.name == '') {
        return alert('输入点东东哦,宝')
      }
      this.list.push(obj)
    },
    del (id) {
      this.list = this.list.filter(item => item.id != id)
    },

  },
  watch: {
    list: {
      handler (val) {
        localStorage.setItem('list', JSON.stringify(val))
      },
      deep: true
    }
  }
}
</script>

<style>
</style>
